package com.hgx.fink.statrt01;

import org.apache.flink.api.common.typeinfo.TypeHint;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class StreamWordCount2 {

    public static void main(String[] args) throws Exception {

        // 创建流处理执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // 设置并行度，默认值 = 当前计算机的CPU逻辑核数（设置成1即单线程处理）
        // env.setMaxParallelism(32);

        // 从文件中读取数据
//        String inputPath = "D:\\hgxDevProject\\flink-abc\\fink-start01\\src\\main\\resources\\hello.txt";
//        DataStream<String> inputDataStream = env.readTextFile(inputPath);

        // 从socket文本流读取数据
        DataStream<String> inputDataStream = env.socketTextStream("106.14.217.80", 8500);

        // 基于数据流进行转换计算
//        DataStream<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(new WordCount.MyFlatMapper())
//                .keyBy(item -> item.f0)
//                .sum(1);
        DataStream<Tuple2<String, Integer>> resultStream = inputDataStream.flatMap(
                (s, out) -> {
                    // 按空格分词
                    String[] words = s.split(" ");
                    // 遍历所有word，包成二元组输出
                    for (String str : words) {
                        out.collect(new Tuple2<>(str, 1));
                    }
                }, TypeInformation.of(new TypeHint<Tuple2<String, Integer>>() {
                })
        ).keyBy(item -> item.f0).sum(1);


        resultStream.print();


        // 执行任务
        env.execute();
    }


}
